package com.probiz.estore.customer.dao.impl;

import java.util.List;

import com.probiz.estore.common.model.customer.Address;
import com.probiz.estore.core.dao.impl.HibernateGenericDaoImpl;
import com.probiz.estore.customer.dao.AddressDao;

/**
 * Address Data Access Object (DAO) implementation. Developer introduced interfaces should be declared here. Won't get overwritten.
 */
public class AddressDaoImpl extends HibernateGenericDaoImpl<Address> implements AddressDao {

	protected void initFilterHqlMap() {
		filterHqlMap.put(Address.FilterHqlName.MY_ADDRESS_LIST, "from Address a where a.appUser.appuserId=?");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.customer.dao.AddressDao#getAllByAppuserId(java.lang.Integer)
	 */
	public List<Address> getAllByAppuserId(Integer appuserId) {
		return findByHql(" from Address vo where vo.appUser.appuserId=?", appuserId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.customer.dao.AddressDao#getAppuserOtherAddress(java.lang.Integer, java.lang.Integer)
	 */
	public List<Address> getAppuserOtherAddress(Integer appuserId, Integer addressId) {
		String hql = "from Address vo where vo.appUser.appuserId=?";
		Object[] params = new Object[] { appuserId };
		if (addressId != null) {
			hql += " and addressId!=?";
			params = new Object[] { appuserId, addressId };
		}
		return findByHql(hql, params);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.customer.dao.AddressDao#getDefShippingAddr(java.lang.Integer)
	 */
	public Address getDefShippingAddr(Integer appuserId) {
		String hql = "from Address vo where vo.appUser.appuserId=? and vo.isDefaultShippingAddress=1 order by addressId desc";
		List<Address> list = super.findByHql(hql, appuserId);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}

		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.customer.dao.AddressDao#getAllShippingAddressByAppuserId(java.lang.Integer)
	 */
	public List<Address> getAllShippingAddressByAppuserId(Integer appuserId) {
		return findByHql(" from Address vo where vo.addressType=1 and vo.appUser.appuserId=?", appuserId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.customer.dao.AddressDao#countAddressOfCustomer(java.lang.Integer)
	 */
	public Long countAddressOfCustomer(Integer customerId) {
		return this.countByFilter(Address.FilterHqlName.MY_ADDRESS_LIST, customerId);
	}
}